package com.sample.javabean;
/**
 * 就诊信息类
 * @author 深深
 * @version 1.0
 */
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sample.action.form.MxillmessageForm;
import com.sample.jdbc.JdbcUtils;
import com.sample.util.Pager;

public class Mxillmessage {
	private static final Log LOG=LogFactory.getLog(Mxillmessage.class);
	private JdbcUtils jdbcUtils;
	private String doctorid;//就诊编号
	private String cardNo;//身份证号
	private String illname;//疾病名称
	private String hospital;//就诊所在医院
	private String illtime;//就诊时间
	private String money;//就诊总费用
	
	public Mxillmessage() {
		jdbcUtils =new JdbcUtils();//实例化连接
	}
	//get/set方法
	public String getDoctorid() {
		return doctorid;
	}
	public void setDoctorid(String doctorid) {
		this.doctorid = doctorid;
	}
	public String getCardNo() {
		return cardNo;
	}
	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}
	public String getIllname() {
		return illname;
	}
	public void setIllname(String illname) {
		this.illname = illname;
	}
	public String getHospital() {
		return hospital;
	}
	public void setHospital(String hospital) {
		this.hospital = hospital;
	}
	public String getIlltime() {
		return illtime;
	}
	public void setIlltime(String illtime) {
		this.illtime = illtime;
	}
	public String getMoney() {
		return money;
	}
	public void setMoney(String money) {
		this.money = money;
	}
	
	//构造方法
	public Mxillmessage(String doctorid, String cardNo, String illname,
			String hospital, String illtime, String money) {
		super();
		this.doctorid = doctorid;
		this.cardNo = cardNo;
		this.illname = illname;
		this.hospital = hospital;
		this.illtime = illtime;
		this.money = money;
	}
	
	//查询所有就诊信息
	public List<Mxillmessage> findAll(){
		Connection conn=null;
		Statement stmt =null;
		ResultSet rs =null;
		List<Mxillmessage> mxillmessageList =new ArrayList<Mxillmessage>(20);
		try{
			 conn =jdbcUtils.getConn();
			 stmt =conn.createStatement();
			//构造SQL语句
			String sql="select * from t_mxill_message where 1=1";
			 rs =stmt.executeQuery(sql);
			while(rs.next()){
				String doctorid=rs.getString("doctorid");
				String cardNo =rs.getString("cardNo");
				String illname =rs.getString("illname");
				String hospital=rs.getString("hospital");
				String illtime =rs.getString("illtime");
				String money =rs.getString("money");
				Mxillmessage model=new Mxillmessage(doctorid, cardNo,illname,hospital,illtime,money);
				mxillmessageList.add(model);
				
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			LOG.error("list mxill_message  failed!", e);
		}finally{
			jdbcUtils.close(conn, stmt,rs);
		}
		return mxillmessageList;
		
	}
	
	/**
	 * 加载就诊信息
	 * @throws Exception
	 */
	public void load()throws Exception{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs =null; 
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "select * from  t_mxill_message where doctorid='"+this.doctorid+"'";
			// 4:执行SQL并返回结果
			rs=stmt.executeQuery(sql);
			if(rs.next()){
				this.doctorid =rs.getString("doctorid");
				this.cardNo =rs.getString("cardNo");
				this.illname =rs.getString("illname");
				this.hospital =rs.getString("hospital");
				this.illtime =rs.getString("illtime");
				this.money =rs.getString("money");
				
			}
			// 5:关闭资源
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SQLException("加载就诊信息失败！");
		} finally {
			jdbcUtils.close(conn, stmt);
		}	
	}
	/**
	 * 编辑就诊信息
	 */
	public void editMxillmessage() {

		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "update  t_mxill_message set doctorid='"+this.doctorid+"',cardNo='"+this.cardNo+"',illname='"+this.illname+"',hospital='"+this.hospital+"',illtime='"+this.illtime+"',money='"+this.money+"' where doctorid='"+this.doctorid+"'";
			// 4:执行SQL并返回结果
			stmt.executeUpdate(sql);
			// 5:关闭资源
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			jdbcUtils.close(conn, stmt);
		}

	}
	/**
	 * 删除就诊信息
	 */
	public void delMxillmessage() {

		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "delete from t_mxill_message  where doctorid='"+this.doctorid+"'";
			// 4:执行SQL并返回结果
			System.out.println("del mxill_message");
			stmt.executeUpdate(sql);
			// 5:关闭资源
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			jdbcUtils.close(conn, stmt);
		}

	}
	/**
	 * 添加就诊信息
	 */
	public void add(){
		Connection conn =null;
		Statement stmt=null;
		try{
			//获得连接对象	
			 conn =jdbcUtils.getConn();
			//定义SQL
			String sql="insert into t_mxill_message(doctorid,cardNo,illname,hospital,illtime,money) values('"+this.doctorid+"','"+this.cardNo+"','"+this.illname+"','"+this.hospital+"','"+this.illtime+"','"+this.money+"')";
			//Statement发送SQL语句给数据库
			 stmt =conn.createStatement();
			stmt.executeUpdate(sql);
		}catch (Exception e) {
			e.printStackTrace();
			
		}finally{
			jdbcUtils.close(conn, stmt);
			
		}		
	}
	/**
	 * 分页显示就诊信息
	 * @param page
	 * @param size
	 * @param form
	 * @return
	 */
	public Pager<Mxillmessage> list(int page,int size,MxillmessageForm form) {

		Connection conn = null;
		Statement stmt = null;
		ResultSet rs =null;
		Pager<Mxillmessage> results=new Pager<Mxillmessage>();
		results.setPage(page);
		results.setSize(size);
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			int start=(page-1)*size;
			int limit=size;
			//查询SQL
			String selectSql="select * from t_mxill_message where 1=1";
			//模糊查询就诊病人的身份证号
			if(form!=null&&form.getCardNo()!=null&&!"".equals(form.getCardNo())){
				selectSql +=" and cardNo like'%"+form.getCardNo()+"%'";
			}
			
			//统计SQL
			String countSql ="select count(*)as c from ("+selectSql+") as t1";
			//分页SQL
			 String pageSql=selectSql+" limit "+start+","+limit;
			 rs= stmt.executeQuery(countSql);
			 int total=0;
			 while(rs.next()){
				 total=rs.getInt("c");
			 }
			 results.setTotal(total);
			 //查询分页信息
			 rs= stmt.executeQuery(pageSql);
			 while(rs.next()){
			
			
				    String doctorid=rs.getString("doctorid");
					String cardNo =rs.getString("cardNo");
					String illname =rs.getString("illname");
					String hospital=rs.getString("hospital");
					String illtime =rs.getString("illtime");
					String money =rs.getString("money");
				  
				  
				
				  Mxillmessage model=new Mxillmessage();
				  model.setDoctorid(doctorid);
				  model.setCardNo(cardNo);
				  model.setIllname(illname);
				  model.setHospital(hospital);
				  model.setIlltime(illtime);
				  model.setMoney(money);
				  results.getDataSets().add(model);
				  
			 }
			 results.init();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			jdbcUtils.close(conn, stmt);
		}
		return results;

	}
	
	

}